package com.laicunba.ext.web.init;

import java.lang.reflect.Method;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestMapping;

import com.laicunba.constant.GloabConstant;
import com.laicunba.ext.web.controller.BaseController;
import com.laicunba.init.SpringInitListener;
import com.laicunba.pojo.ManagerUserDO;
import com.laicunba.service.ActionService;
import com.laicunba.service.ManagerUserService;
import com.laicunba.service.MenuService;
import com.laicunba.service.TableAutoCreateService;
import com.laicunba.util.AssertUtil;
import com.laicunba.util.SessionUtil;
import com.laicunba.util.StringUtil;

@Component
public class SpringApplicationListener implements ApplicationListener {
	private static final Logger LOG = Logger.getLogger(SpringApplicationListener.class);
	@Autowired
	private BaseController[] controllerList;
	private static boolean IS_INITED_MENU = false;
	@Autowired
	private ActionService actionService;
	@Autowired
	private MenuService menuService; 
	@Autowired
	private ManagerUserService managerUserService;

	@SuppressWarnings("unchecked")
	@Override
	public void onApplicationEvent(ApplicationEvent event) {
		if (SpringInitListener.NEED_CREATE_TAB) {
			SpringInitListener.NEED_CREATE_TAB = false;
			String webPath = GloabConstant.WEB_PATH;
		}
		/*
		 * 反射所有的Controller类,获得所有的uri,然后如果数据库中不存在菜单或Action就存进去
		 */

		if (!IS_INITED_MENU && controllerList != null) {
			try {
				SessionUtil.CURRENT_USER_ID.set(GloabConstant.ROOT_USER_ID_AND_NAME);
				IS_INITED_MENU = true;
				LOG.info("初始化菜单");

				String classMappingPath = "";
				String methodMappingPath = "";
				RequestMapping classMapping;
				RequestMapping methodMapping;
				for (BaseController controller : controllerList) {
					System.out.println(controller);
					classMapping = controller.getClass().getAnnotation(RequestMapping.class);
					if (classMapping != null) {
						classMappingPath = classMapping.value()[0];
					} else {
						classMappingPath = "";
					}
					// 查找所有应映路径的方法
					for (Method method : controller.getClass().getMethods()) {
						methodMapping = method.getAnnotation(RequestMapping.class);
						if (methodMapping == null) {
							continue;
						}
						methodMappingPath = methodMapping.value()[0];
						String uri = classMappingPath + methodMappingPath;
						if (!uri.startsWith("/console/")) {
							continue;
						}
						String actionId = StringUtil.md5(uri);

						PropertiesInit.CONSOLE_URI.put(actionId, uri);

					}

				}
				System.out.println(PropertiesInit.CONSOLE_URI);
				// 查看数据库里面,是否有ROOT账号,如果没有则创建
				ManagerUserDO rootUser = managerUserService.get(ManagerUserDO.class,
						GloabConstant.ROOT_USER_ID_AND_NAME);
				if (rootUser == null) {
					rootUser = new ManagerUserDO();
					rootUser.setId(GloabConstant.ROOT_USER_ID_AND_NAME);
					rootUser.setLoginName(GloabConstant.ROOT_USER_ID_AND_NAME);
					rootUser.setLoginPwd(StringUtil.md5(GloabConstant.ROOT_USER_ID_AND_NAME));
					rootUser.setStatus(1);
					AssertUtil.assertTrue(managerUserService.create(rootUser));
				}
			} finally {
				SessionUtil.CURRENT_USER_ID.remove();
			}
		}

	}
}
